package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.model.FileGenInfo;
import com.ruoyi.generator.domain.model.FileGenQueryInfo;
import com.ruoyi.generator.service.IGenHtmlService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代码生成 操作处理
 *
 * @author ruoyi
 */
@Anonymous
@RestController
@RequestMapping("/tool/gen/html")
public class GenHtmlController extends BaseController {

    @Autowired
    private IGenHtmlService genHtmlService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 查询要生成的HTML列表
     */
    @PreAuthorize("@ss.hasPermi('tool:gen:code')")
    @Log(title = "代码生成" , businessType = BusinessType.GENCODE)
    @GetMapping("/htmlDataList")
    public TableDataInfo htmlDataList(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 生成HTML（自定义路径）
     */
   //  @PreAuthorize("@ss.hasPermi('tool:gen:code')")
    @Log(title = "代码生成" , businessType = BusinessType.GENCODE)
    @PostMapping("/genHTMLCode")
    public AjaxResult genHTMLCode(@RequestBody FileGenInfo fileGenInfo) {
        genHtmlService.generatorHTMLCode(fileGenInfo);
        return success();
    }

    /**
     * 生成HTML（自定义路径）
     */
    @PreAuthorize("@ss.hasPermi('tool:gen:code')")
    @Log(title = "代码生成" , businessType = BusinessType.GENCODE)
    @PostMapping("/batchGenHTMLCode")
    public AjaxResult batchGenHTMLCode(@RequestBody List<String> pageTypes) {
        for (String pageType : pageTypes) {
            threadPoolTaskExecutor.submit(() -> {
                FileGenInfo fileGenInfo = new FileGenInfo();
                fileGenInfo.setPageType(pageType);
                genHtmlService.generatorHTMLCode(fileGenInfo);
            });
        }
        return success("页面生成中,请等待");
    }
}